//
//  AppSettings.swift
//  RabbleWabble
//
//  Created by TRS-Mobile on 2021/9/8.
//

import Foundation

class AppSettings {
    public static let shared = AppSettings()
    
    private let userDefaults = UserDefaults.standard
    
    var questionStrategyType: QuestionStrategyType {
        get {
            let rawValue = userDefaults.integer(forKey: Keys.questionStrategy)
            return QuestionStrategyType(rawValue: rawValue)!
        }
        
        set {
            userDefaults.setValue(newValue.rawValue, forKey: Keys.questionStrategy)
        }
    }
    
//    func questionStrategy(for questionGroup: QuestionGroup) -> QuestionStrategy {
//        return questionStrategyType.questionStrategy(for: questionGroup)
//    }
    
    func questionStrategy(forQuestion caretaker: QuestionGroupCaretaker) -> QuestionStrategy {
        return questionStrategyType.questionStrategy(forQuestion: caretaker)
    }
}

enum QuestionStrategyType: Int, CaseIterable {
    case random
    case sequential
    
    var title: String {
        switch self {
        case .random:
            return "Random"
        case .sequential:
            return "Sequential"
        }
    }
    
//    func questionStrategy(for questionGroup: QuestionGroup) -> QuestionStrategy {
//        switch self {
//        case .random:
////            return RandomQuestionStrategy(questionGroup: questionGroup)
//        return RandomQuestionStrategy(caretaker: <#T##QuestionGroupCaretaker#>)
//        case .sequential:
//            return SequentialQuestionStrategy(questionGroup: questionGroup)
//        }
//    }
    
    func questionStrategy(forQuestion caretaker: QuestionGroupCaretaker) -> QuestionStrategy {
        switch self {
        case .random:
            return RandomQuestionStrategy(caretaker: caretaker)
        case .sequential:
            return SequentialQuestionStrategy(caretaker: caretaker)
        }
    }
}

fileprivate struct Keys {
    static let questionStrategy = "questionStrategy"
}
